package com.bit.springmvc.demo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @PostMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        //1. 参数校验
        //2. 验证密码是否正确
        //3. 进行返回

//        if (userName==null || "".equals(userName) || password==null || "".equals(password)) {
//            return false;
//        }
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //验证密码是否正确, 理论来说, 应该去访问数据库
        //暂且硬编码, 写死
        if ("admin".equals(userName) && "admin".equals(password)){
            session.setAttribute("loginUserName", userName);
            return true;
        }
        return false;
    }

    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session==null){
            return "";
        }
        return (String)session.getAttribute("loginUserName");
    }
}
